package cn.raven.system.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.system.service.IMenuService;
import cn.raven.system.domain.Menu;
import cn.raven.system.query.MenuQuery;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存菜单",sn = "menu:save")
    public AjaxResult save(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除菜单",sn = "menu:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            menuService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "id查询菜单",sn = "menu:id")
    public Menu findById(@PathVariable("id")Long id)
    {
        return menuService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Menu> findAll(){

        return menuService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "菜单列表",sn = "menu:list")
    public PageList<Menu> queryData(@RequestBody MenuQuery query)
    {
        return menuService.queryData(query);
    }
    /** 
    * @Title: tree
    * @Description: 上级菜单树
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/11 15:35  
    * @Parameters: []
    * @Return java.util.List<cn.raven.system.domain.Menu>
      */
    @GetMapping("/tree")
    @PreAuthorize(name = "上级菜单树",sn = "menu:tree")
    public List<Menu> tree()
    {
        return menuService.tree();
    }

    @PatchMapping
    @PreAuthorize(name = "批量删除菜单",sn = "menu:batchDelete")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            menuService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
}
